// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
	$ionicPlatform.ready(function() {
		if (window.cordova && window.cordova.plugins.Keyboard) {
			// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
			// for form inputs)
			cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

			// Don't remove this line unless you know what you are doing. It stops the viewport
			// from snapping when text inputs are focused. Ionic handles this internally for
			// a much nicer keyboard experience.
			cordova.plugins.Keyboard.disableScroll(true);
		}
		if (window.StatusBar) {
			StatusBar.styleDefault();
		}
	});
})

.config(function($stateProvider, $urlRouterProvider) {

	// Ionic uses AngularUI Router which uses the concept of states
	// Learn more here: https://github.com/angular-ui/ui-router
	// Set up the various states which the app can be in.
	// Each state's controller can be found in controllers.js
	$stateProvider
		.state('index', {
			url: "/search",
			templateUrl: "search.html",
			controller: 'searchCtrl'
		})
		.state('home', {
			url: "/home",
			templateUrl: "home.html"
		})
		.state('result', {
			url: "/result",
			abstract: true,
			templateUrl: "result.html",
			controller: 'resultCtrl'
		})
		.state('result.content', {
			url: "/content",
			views: {
				'searchResult': {
					templateUrl: "templates/tabs.html",
					controller: 'contentCtrl'
				}
			}
		})
		.state('result.content.knowledge', {
			url: '/knowledge',
			views: {
				'tab-knowledge': {
					templateUrl: 'templates/tab-knowledge.html',
					controller: 'KnowledgeCtrl'
				}
			}
		})
		.state('result.content.bbs', {
			url: '/bbs',
			views: {
				'tab-bbs': {
					templateUrl: 'templates/tab-bbs.html',
					controller: 'BbsCtrl'
				}
			}
		})
		.state('answer', {
			url: '/answer',
			abstract: true,
			templateUrl: 'bbsAnswer.html'
		})
		.state('answer.content', {
			url: '/content',
			views: {
				'bbsAnswer': {
					templateUrl: 'templates/bbsContent.html',
					controller: 'BbsContentCtrl'
				}
			}
		})
		.state('result.content.expert', {
			url: '/expert',
			views: {
				'tab-expert': {
					templateUrl: 'templates/tab-expert.html',
					controller: 'ExpertCtrl'
				}
			}
		});

	$urlRouterProvider.otherwise("/search");

})

.controller('MyCtrl', function($scope) {

	var FAQData1 = [{
		color: "button-calm",
		text: "不吃饭"
	}, {
		color: "button-royal",
		text: "孩子不睡觉咋整"
	}, {
		color: "button-energized",
		text: "不开心"
	}, {
		color: "button-balanced",
		text: "孩子不打豆豆怎么办"
	}, {
		color: "button-assertive",
		text: "几岁才能不尿床"
	}, {
		color: "button-positive",
		text: "怎么打孩子他才听话"
	}];

	var FAQData2 = [{
		color: "button-calm",
		text: "还不能走路"
	}, {
		color: "button-royal",
		text: "不会说话"
	}, {
		color: "button-energized",
		text: "怎么还不换牙"
	}, {
		color: "button-balanced",
		text: "拉稀"
	}, {
		color: "button-assertive",
		text: "最近怎么不爱吃奶了"
	}, {
		color: "button-positive",
		text: "每天不喝奶就要哭"
	}];
	$scope.FAQ = FAQData1;

	$scope.history = ["不收拾玩具", "頑皮", "不吃青菜"];

	$scope.doRefreshFAQ = function() {
		if ($scope.FAQ == FAQData1) {
			$scope.FAQ = FAQData2;
		} else {
			$scope.FAQ = FAQData1;
		}
	};

	$scope.historyClear = function() {
		$scope.history = [];
	};

})

.controller('KnowledgeCtrl', function($scope) {
	$scope.answers = [{
		title: "真的不打疫苗了吗？保持理智实话实说",
		img: "img/pic00.jpg",
		describe: "“未冷藏疫苗流入市场”这条新闻出来好几天，小编一直克制着愤慨，想从更科学客观的角度解读。",
		date: "2016-03-03",
		readNum: 5525,
		praiseNum: 3527
	}];

})

.controller('BbsCtrl', function($scope, $timeout, $ionicLoading, $sce) {

	$scope.ifLoad = false;

	$ionicLoading.show({
		content: 'Loading',
		animation: 'fade-in',
		showBackdrop: true,
		maxWidth: 200,
		showDelay: 0
	});

	$timeout(function() {
		$ionicLoading.hide();
		$scope.ifLoad = true;
		$scope.answers = [{
			questionId: 001,
			questionTime: "4个小时前",
			forumNum: 582,
			asker: {
				name: "曦宝贝的妈咪",
				head: "img/head-pic00.png",
				question: "孩子不打疫苗行不行？",
				describe: "对疫苗这块的认知为0，但是我想知道，如果孩子从未打过疫苗，是不是可行的？"
			},
			optimum: {
				name: "",
				answer: "先写结论：完全不可行<br>楼主你的逻辑是很典型的幸存者偏差，那些没接种疫苗而感染死亡的患者，他们根本没机会成为你的父辈。。。。。。",
				praiseNum: 1463
			}
		}, {
			questionId: 002,
			questionTime: "6个小时前",
			forumNum: 563,
			asker: {
				name: "家有小美女宝宝",
				head: "img/head-pic01.png",
				question: "幼儿注射非冷藏疫苗有什么危害？",
				describe: "本次山东非冷藏疫苗涉及的儿童很多，请问有哪些危害，注射了这些疫苗的家长怎么办？新闻只公布。。。。。。"
			},
			optimum: {
				name: "",
				answer: "首先，冷链运输断裂的疫苗和不合格疫苗是两回事。冷链运输断裂的疫苗会失效，造成的危害就和你没打疫苗一样，补救措施就是从打一针合格的疫苗。。。。。。",
				praiseNum: 963
			}
		}, {
			questionId: 001,
			questionTime: "4个小时前",
			forumNum: 582,
			asker: {
				name: "曦宝贝的妈咪",
				head: "img/head-pic00.png",
				question: "孩子不打疫苗行不行？",
				describe: "对疫苗这块的认知为0，但是我想知道，如果孩子从未打过疫苗，是不是可行的？"
			},
			optimum: {
				name: "",
				answer: "先写结论：完全不可行</br>楼主你的逻辑是很典型的幸存者偏差，那些没接种疫苗而感染死亡的患者，他们根本没机会成为你的父辈。。。。。。",
				praiseNum: 1463
			}
		}, {
			questionId: 002,
			questionTime: "6个小时前",
			forumNum: 563,
			asker: {
				name: "家有小美女宝宝",
				head: "img/head-pic01.png",
				question: "幼儿注射非冷藏疫苗有什么危害？",
				describe: "本次山东非冷藏疫苗涉及的儿童很多，请问有哪些危害，注射了这些疫苗的家长怎么办？新闻只公布。。。。。。"
			},
			optimum: {
				name: "",
				answer: "首先，冷链运输断裂的疫苗和不合格疫苗是两回事。冷链运输断裂的疫苗会失效，造成的危害就和你没打疫苗一样，补救措施就是从打一针合格的疫苗。。。。。。",
				praiseNum: 963
			}
		}];
		
		for (var i in $scope.answers)
		{
			$scope.answers[i].optimum.answer = $sce.trustAsHtml($scope.answers[i].optimum.answer);
		}
	}, 1000);

	

	$scope.moreDataCanBeLoaded = function() {

		return $scope.ifLoad;
	};

	$scope.doAddBbs = function() {

		var newContent = [{
			questionId: 002,
			questionTime: "6个小时前",
			forumNum: 563,
			asker: {
				name: "你是新来的吧",
				head: "img/head-pic01.png",
				question: "本色臻品本色臻品本色臻品本色臻品本色臻品？",
				describe: "纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人"
			},
			optimum: {
				name: "",
				answer: "首先，冷链运输断裂的疫苗和不合格疫苗是两回事。冷链运输断裂的疫苗会失效，造成的危害就和你没打疫苗一样，补救措施就是从打一针合格的疫苗。。。。。。",
				praiseNum: 963
			}
		}, {
			questionId: 002,
			questionTime: "6个小时前",
			forumNum: 563,
			asker: {
				name: "你是新来的吧",
				head: "img/head-pic01.png",
				question: "本色臻品本色臻品本色臻品本色臻品本色臻品？",
				describe: "纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人"
			},
			optimum: {
				name: "",
				answer: "首先，冷链运输断裂的疫苗和不合格疫苗是两回事。冷链运输断裂的疫苗会失效，造成的危害就和你没打疫苗一样，补救措施就是从打一针合格的疫苗。。。。。。",
				praiseNum: 963
			}
		}, {
			questionId: 002,
			questionTime: "6个小时前",
			forumNum: 563,
			asker: {
				name: "你是新来的吧",
				head: "img/head-pic01.png",
				question: "本色臻品本色臻品本色臻品本色臻品本色臻品？",
				describe: "纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人纸给最爱的人"
			},
			optimum: {
				name: "",
				answer: "首先，冷链运输断裂的疫苗和不合格疫苗是两回事。冷链运输断裂的疫苗会失效，造成的危害就和你没打疫苗一样，补救措施就是从打一针合格的疫苗。。。。。。",
				praiseNum: 963
			}
		}];

		$timeout(function() {
			for (var i = 0; i < newContent.length; i++) {
				$scope.answers.push(newContent[i]);
			}
			//		$scope.$broadcast("scroll.refreshComplete");
			$scope.$broadcast("scroll.infiniteScrollComplete");
		}, 1000);
	};

})

.controller('ExpertCtrl', function($scope) {
	$scope.settings = {
		enableFriends: true
	};

	$scope.answers = [{
		name: "崔玉涛",
		img: "img/pic01.jpg",
		title: "关于【问题疫苗】的所有问题，需要一个客观的回答",
		describe: "今天朋友圈里关于【问题疫苗】的各种观点想必大家已经看糊涂了。我们把大家最关注的问题整理出来，一一回复。",
		date: "2016-03-23",
		readNum: 6326,
		praiseNum: 4274
	}];
})

.controller('searchCtrl', function($scope) {

	$scope.faqSearchClearCopy = function() {
		$scope.faqSearchCopy = "";
	};
})

.controller('contentCtrl', function($scope) {

})

.controller('resultCtrl', function($scope) {
	$scope.faqSearchClear = function() {
		$scope.faqSearch = "";
	};
})

.controller('BbsContentCtrl', function($scope, $ionicPopup, $sce) {

	//  alert（警告） 对话框
	$scope.showAlert = function(id) {
		var alertPopup = $ionicPopup.alert({
			title: '提示：',
			template: '你的举报已经记录，管理员稍后会进行处理'
		});
		alertPopup.then(function(res) {
			console.log('');
		});
	};

	$scope.question = {
		qId: 1,
		qUserId: "0001",
		qName: "曦宝贝的妈咪",
		qHead: "img/head-pic00.png",
		qTitle: "孩子不打疫苗行不行",
		qContent: "对疫苗这块的认知为0，但是我想知道，如果孩子从来未打过疫苗，是不是可行的？",
		qDateTime: "4小时前",
		qCollectNum: 1350,
		qAgree: 1463,
		qAnswerNum: 582
	};

	$scope.answers = [{
		aId: 1,
		aUserId: "000001",
		aName: "喵吖喵",
		aHead: "img/head-pic00.png",
		aDateTime: "2016-03-24 13:57",
		aAgreeNum: 245,
		aContent: "先写结论：完全不可行 <br/>楼主你的逻辑是很典型的幸存者偏差，那些没接种疫苗而感染死亡的患者，他们根本没机会成为你的父辈</br>为什么要通过打疫苗预防急性传染病？</br>急性传染病起病急，进展快",
		aIsLong: false,
		aMoreContent: "",
		aAnswerNum: 156,
		aAnswerId: false,
		aAnswerName: "",
		aAnswerContent: "",
		aAnswerIsLong: false,
		aAnswerMoreContent: ""
	}, {
		aId: 2,
		aUserId: "000002",
		aName: "家有小美女宝宝",
		aHead: "img/head-pic01.png",
		aDateTime: "2016-03-24 13:57",
		aAgreeNum: 167,
		aContent: "我觉得大家都没有说到重点，并没有解决题主的疑问。",
		aIsLong: false,
		aMoreContent: "",
		aAnswerNum: 70,
		aAnswerId: "1",
		aAnswerName: "喵吖喵",
		aAnswerContent: "先写结论：完全不可行</br>楼主你的逻辑是很典型的幸存者偏差，那些没接种疫苗而感染死亡的患者，他们根本没机会成为你的父辈<\/br>为什么要通过打疫苗预防急性传染病？</br>急性传染病起病急，进展快",
		aAnswerIsLong: false,
		aAnswerMoreContent: ""
	}];

	$scope.showMore = function(site, id) {
		if (site == "up") {
			$scope.answers[id].aIsLong = false;
		} else {
			$scope.answers[id].aAnswerIsLong = false;
		}

	};

	var ALLOW_MAX = 60;
	for (var i in $scope.answers) {
		if ($scope.answers[i].aContent.length > ALLOW_MAX) {
			$scope.answers[i].aIsLong = true;
			$scope.answers[i].aMoreContent = $scope.answers[i].aContent.substring(ALLOW_MAX);
			$scope.answers[i].aMoreContent = $sce.trustAsHtml($scope.answers[i].aMoreContent);
			$scope.answers[i].aContent = $scope.answers[i].aContent.substring(0, ALLOW_MAX);
			$scope.answers[i].aContent = $sce.trustAsHtml($scope.answers[i].aContent);
		}

		if ($scope.answers[i].aAnswerContent.length > ALLOW_MAX) {
			$scope.answers[i].aAnswerIsLong = true;
			$scope.answers[i].aAnswerMoreContent = $scope.answers[i].aAnswerContent.substring(ALLOW_MAX);
			$scope.answers[i].aAnswerMoreContent = $sce.trustAsHtml($scope.answers[i].aAnswerMoreContent);
			$scope.answers[i].aAnswerContent = $scope.answers[i].aAnswerContent.substring(0, ALLOW_MAX);
			$scope.answers[i].aAnswerContent = $sce.trustAsHtml($scope.answers[i].aAnswerContent);
		}
	}
})